# -*- coding: utf-8 -*-
# @Author : y
# @Date   : 2021/6/09
# @File   : Rule9_EntLimitIndusCheck.py
# @E-mail : yantao@bbdservice.com

'''eof
name:申请企业所在行业为两高一剩行业
code:Rule9_EntLimitIndusCheck
tableName:
columnName:
groups:企业通用模块
dependencies:EG_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import pandas as pd
import json
from datetime import datetime


def format_data(tableName):
    try:
        eg = EG_DSJ.get("data").get("R1103V3")[0].get(tableName)
        return eg if isinstance(eg, list) else [{}]
    except:
        return [{}]


# 统一转成datetime对象
def date_format(time):
    import re
    r = re.compile(r'\D+')
    try:
        date = datetime.strptime(time, '%Y.%m.%d')
    except:
        try:
            date = datetime.strptime(time, '%Y-%m-%d')
        except:
            try:
                date = datetime.strptime(time, '%Y/%m/%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.strptime(tmp, '%Y%m%d')
                except:
                    date = None
    return date


def get_date_list():
    try:
        now = datetime.now()
        aaa = [(now + pd.tseries.offsets.DateOffset(months=i)).strftime('%Y%m%d') for i in [-12, 0]]
        return aaa
    except:
        return None


def get_result():
    '''
    申请企业所在行业在行内定义的两高一剩或行内黑名单行业中,intercept_list暂未确定
    '''
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    #-------------------------------------------
    intercept_list=[]
    try:
        basic_list = format_data("basicList")
        if basic_list == [{}]:
            return u'缺失值'
        else:
            if len(basic_list) >= 1:
                tmp=[]
                for bl in basic_list:
                    industry_phyName=bl.get('industryPhyName')
                    if industry_phyName in null_type_list:
                        tmp.append(1)
                    if industry_phyName in intercept_list:
                        return 1
                return u'缺失值' if len(tmp)==len(basic_list) else 0
            else:
                return 0
    except:
        return u'缺失值'


if __name__ == "__main__":
    file_obj = open(r'E:\Z2需求文档\需求文档\重庆\工商\EG_DSJ.json', encoding='utf-8', mode='r')
    #file_obj = open(r'./EG_DSJ.json', encoding='utf-8', mode='r')
    content = file_obj.read()
    EG_DSJ = json.loads(content, strict=False)
    aa = get_result()
    print(aa)

result = get_result()
